package com.hsh.tools.convert;

import com.hsh.entity.SysDictData;
import com.hsh.tools.AcCollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 字典数据构建类
 * </p>
 *
 * @author huangsihai
 * @date 8/29/2021 11:21 PM
 */
public class DictTreeBuilder {

    /**
     * 构建某一父节点的 tree
     */
    public static List<SysDictData> buildChild(List<SysDictData> nodes, Long parentId) {
        // 指定节点 ID
        List<SysDictData> rootMenu = new ArrayList<>(nodes.size());
        for (SysDictData nav : nodes) {
            if (nav.getId().equals(parentId)) {
                rootMenu.add(nav);
            }
        }
        // 排序
        rootMenu.sort(SysDictData.order());
        for (SysDictData nav : rootMenu) {
            nav.setChildren(getChild(nodes, parentId));
        }
        return rootMenu;
    }

    /**
     * 构建多级 tree
     */
    public static List<SysDictData> buildTree(List<SysDictData> nodes) {
        List<SysDictData> rootMenu = new ArrayList<>(nodes.size());
        for (SysDictData nav : nodes) {
            // 指定节点构建
            if (nav.getParentId().equals(0L)) {
                rootMenu.add(nav);
            }
        }
        // 排序
        rootMenu.sort(SysDictData.order());
        for (SysDictData nav : rootMenu) {
            nav.setChildren(getChild(nodes, nav.getId()));
        }
        return rootMenu;
    }

    /**
     * 获取指定父节点的字节点
     */
    private static List<SysDictData> getChild(List<SysDictData> nodes, Long parentId) {
        // 子菜单
        List<SysDictData> childList = new ArrayList<>(nodes.size());
        for (SysDictData nav : nodes) {
            if (nav.getParentId().equals(parentId)) {
                childList.add(nav);
            }
        }
        for (SysDictData nav : childList) {
            nav.setChildren(getChild(nodes, nav.getId()));
        }
        // 排序
        childList.sort(SysDictData.order());
        if (childList.size() == 0) {
            return Collections.emptyList();
        }
        return childList;
    }

    /**
     * tree转list
     */
    public static List<SysDictData> treeToList(List<SysDictData> nodes) {
        List<SysDictData> result = new ArrayList<>();
        for (SysDictData node : nodes) {
            result.add(node);
            List<SysDictData> childList = node.getChildren();
            if (AcCollectionUtils.isNotEmpty(childList)) {
                result.addAll(treeToList(childList));
            }
        }
        if (AcCollectionUtils.isNotEmpty(result)) {
            for (SysDictData entity : result) {
                entity.setChildren(Collections.emptyList());
            }
        }
        return result;
    }
    // ===============================业务所需======================================

    /**
     * @param nodes     数据集合
     * @param parentId  父节点 ID
     * @param contentId 内容 ID
     * @return tree list
     */
    public static List<SysDictData> buildTreeContent(List<SysDictData> nodes, Long parentId, Long contentId) {
        List<SysDictData> rootMenu = new ArrayList<>(nodes.size());
        for (SysDictData nav : nodes) {
            if (nav.getParentId().equals(0L) && nav.getId().equals(parentId)) {
                rootMenu.add(nav);
            }
        }
        // 排序
        rootMenu.sort(SysDictData.order());
        for (SysDictData nav : rootMenu) {
            nav.setChildren(getChildContent(nodes, nav.getId(), contentId));
        }
        return rootMenu;
    }

    /**
     * @param nodes     数据集合
     * @param parentId  父节点 ID
     * @param contentId 筛选内容 ID
     * @return list
     */
    private static List<SysDictData> getChildContent(List<SysDictData> nodes, Long parentId, Long contentId) {
        List<SysDictData> childList = new ArrayList<>(nodes.size());
        for (SysDictData nav : nodes) {
            if ((nav.getParentId().equals(parentId))
                    &&
                    (nav.getAncestors().contains(String.valueOf(contentId).toUpperCase().replace("L", "")))
            ) {
                childList.add(nav);
            }
        }
        for (SysDictData nav : childList) {
            nav.setChildren(getChildContent(nodes, nav.getId(), contentId));
        }
        // 排序
        childList.sort(SysDictData.order());
        if (childList.size() == 0) {
            return Collections.emptyList();
        }
        return childList;
    }
    // ===============================end======================================


}
